using UnityEngine;
using System.Collections;

/// \class  gkTestPerformanceSendMessage
/// \brief  Test the performance of send message
[AddComponentMenu("GK/Test/Performance/Message/gkTestPerformanceSendMessage")]
public class gkTestPerformanceSendMessage : MonoBehaviour
{
	/// Send message options
	public SendMessageOptions SendMessageOption = SendMessageOptions.DontRequireReceiver;
	
	/// The key to launch the test
	public KeyCode LaunchTestKey = KeyCode.Alpha2;
	
	/// The number of test loops
	public int NbTestLoops = 1000;
	
	/// \brief  Called each frame
	void Update()
	{
		if(Input.GetKeyDown(LaunchTestKey))
		{
			// Launch the test
			TestPerformance();
		}
	}
	
	/// \brief  The Test
	void TestPerformance()
	{
		print("SendMessageDuration : " + NbTestLoops + " Calls = " + TimeSendMessage() + " ms");
	}
	
	/// \brief  Time send message
	float TimeSendMessage()
	{
		float fSendMessageBeginTime;
		float fSendMessageEndTime;
		
		fSendMessageBeginTime = Time.realtimeSinceStartup * 1000.0f;
		
		// launch the send messages
		for(int i=0; i<NbTestLoops; i++)
		{
			SendMessage("ReceiveMessage", SendMessageOption);
		}
		
		fSendMessageEndTime = Time.realtimeSinceStartup * 1000.0f;
		
		return fSendMessageEndTime - fSendMessageBeginTime;
	}
}